﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Eqwww.App.SystemSecurity;
using Eqwww.App.Authorize;
using Eqwww.Domain.Models.SystemManage;
using Eqwww.Code;
using Eqwww.Web.Areas.SystemSecurity.Models.VersionLog;
using AutoMapper;
using Eqwww.Domain.Models.SystemSecurity;

namespace Eqwww.Web.Areas.SystemSecurity.Controllers
{
    [Area("SystemSecurity")]
    public class VersionLogController : ControllerBase
    {
        private VersionLogApp _versionLogApp;
        private AuthorizeApp _authorizeApp;
        private IMapper _mapper;

        public VersionLogController(VersionLogApp versionLogApp
            , AuthorizeApp authorizeApp
            , IMapper mapper)
        {
            _versionLogApp = versionLogApp;
            _authorizeApp = authorizeApp;
            _mapper = mapper;
        }

        [AuthFilter]
        public IActionResult Index()
        {
            var currentModuleCode = "SystemSecurity_VersionLog";
            ViewData["SelectedButtons"] = _authorizeApp.GetAuthorizedModuleButtons(User, currentModuleCode, ModuleButtonPosition.Selected);
            ViewData["InitialButtons"] = _authorizeApp.GetAuthorizedModuleButtons(User, currentModuleCode, ModuleButtonPosition.Initial);
            return View();
        }

        public IActionResult GridJson(Pagination pagination)
        {
            var data = new
            {
                rows = from p in _versionLogApp.FindIQueryable(pagination)
                       select new IndexViewModel
                       {
                           Id = p.Id,
                           CreateUserName = p.CreateUser == null ? null : p.CreateUser.RealName,
                           CreateTime = p.CreateTime,
                           LastUpdateTime = p.LastUpdateTime,
                           LastUpdateUserName = p.LastUpdateUser == null ? null : p.LastUpdateUser.RealName,
                           Version = p.Version
                       },
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [AuthFilter]
        public IActionResult Details(string id)
        {
            if (string.IsNullOrEmpty(id))
                return Error("id参数不能为空");
            var model = _versionLogApp.FindInclude(id);
            if (model == null)
                return Error("未查询到指定实体");
            return View(model);
        }


        [AuthFilter]
        public IActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult CreateSubmit([Bind("Version, Content")] CreateViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var model = _mapper.Map<VersionLogModel>(viewModel);
                return Json(_versionLogApp.Create(model));
            }
            return Json(Common.ModelStateErrorResult(ModelState));
        }

        [AuthFilter]
        public IActionResult Edit(string id)
        {
            var model = _versionLogApp.Find(id);
            var viewModel = _mapper.Map<EditViewModel>(model);
            return View(viewModel);
        }

        [HttpPost]
        [ExceptionFilter]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> EditSubmitAsync(string id,[Bind("Id,Version, Content")] EditViewModel viewModel)
        {
            if (string.IsNullOrEmpty(id))
                return Error("id参数不能为空");
            if (id != viewModel.Id)
                return Error("id与视图实体Id不一致");
            if (ModelState.IsValid)
            {
                var model = _versionLogApp.Find(id);
                if (model == null)
                    return Error("未查询到指定实体");
                _mapper.Map(viewModel, model);
                return Json(await _versionLogApp.UpdateAsync(model));
            }
            return Json(Common.ModelStateErrorResult(ModelState));
        }

        [HttpPost]
        [ExceptionFilter]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteLogic(string id)
        {
            if (string.IsNullOrEmpty(id))
                return Error("id参数不能为空");
            var model = _versionLogApp.Find(id);
            if (model == null)
                return Error("未查询到指定实体");
            return Json(await _versionLogApp.DeleteLogicAsync(model));
        }
    }
}
